package com.knight.mall.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.knight.mall.dto.EnterpriseDto;
import com.knight.mall.enums.EnterpriseServiceResultEnum;
import com.knight.mall.exception.ServiceException;
import com.knight.mall.mappers.EnterpriseMapper;
import com.knight.mall.model.Enterprise;
import com.knight.mall.query.EnterpriseQuery;
import com.knight.mall.service.EnterpriseService;
import com.knight.mall.utils.DigestsUtils;

@Service(value = "enterpriseService")
public class EnterpriseServiceImpl implements EnterpriseService {
	
	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(EnterpriseServiceImpl.class);
	
	@Value("${store.server.url}")
	private String storeUrl;
	
	@Autowired
	private EnterpriseMapper enterpriseMapper;

	@Override
	public Enterprise load(Long id) {
		Enterprise enterprise = enterpriseMapper.load(id);
		enterprise.setHeadPicUrl(storeUrl + enterprise.getHeadPicUrl());
		return enterprise;
	}

	@Override
	public Long add(Enterprise t) throws ServiceException {
		enterpriseMapper.insert(t);
		return t.getId();
	}

	@Override
	public int update(Enterprise t) {
		return enterpriseMapper.update(t);
	}

	@Override
	public int delete(Long id) {
		return enterpriseMapper.delete(id);
	}

	@Override
	public List<Enterprise> queryList(EnterpriseQuery q) {
		List<Enterprise> list = enterpriseMapper.queryList(q);
		if(list!=null && list.size()>0){
			for(Enterprise e:list){
				if(e.getHeadPicUrl()!=null && !"".equals(e.getHeadPicUrl())){
					e.setAbsoluteHeadPicUrl(storeUrl + e.getHeadPicUrl());
				}
			}
		}
		return list;
	}

	@Override
	public int queryCount(EnterpriseQuery q) {
		return enterpriseMapper.queryCount(q);
	}
	
	/***
	 * 登陆方法
	 * @param dto
	 * @return
	 * @throws ServiceException
	 */
	public Enterprise login(EnterpriseDto dto) throws ServiceException {
		Enterprise user = loadByAccount(dto.getAccount());

		Boolean result = validateUser(user, dto.getSign(), dto.getRandom());

		if (result) {
			logger.debug("用户{}登陆成功",dto.getAccount());
			return user;
		}else{
			return null;
		}

	}
	
	private Boolean validateUser(Enterprise user, String sign, String random) throws ServiceException {
		// 账户不存在
		if (user == null) {
			logger.error(EnterpriseServiceResultEnum._2060501.getMsg());
			throw new ServiceException(EnterpriseServiceResultEnum._2060501.getCode(),EnterpriseServiceResultEnum._2060501.getMsg());
		}

		
		String s1 = DigestsUtils.md5Hex((user.getAccount() + user.getPassword()).getBytes());
		String sign2 = DigestsUtils.md5Hex((s1 + random).getBytes());
		if (!StringUtils.equals(sign, sign2)) {
			throw new ServiceException(EnterpriseServiceResultEnum._2060501.getCode(),EnterpriseServiceResultEnum._2060501.getMsg());
			//handleFailLogin(user,result);
		}
		// 验证通过
		return true;

	}
	
	private Enterprise loadByAccount(String account) {
		EnterpriseQuery query = new EnterpriseQuery();
		query.setAccount(account);
		//通过账号查询
		List<Enterprise> users = this.queryList(query);
		if (users == null || users.size()!=1) {
			//
			return null;
		}else{
			return users.get(0);
		}
	}

	@Override
	public Enterprise getById(Long id) {
		Enterprise enterprise = enterpriseMapper.load(id);
		if(enterprise.getHeadPicUrl()!=null && !"".equals(enterprise.getHeadPicUrl())){
			enterprise.setAbsoluteHeadPicUrl(storeUrl + enterprise.getHeadPicUrl());
		}
		return enterprise;
	}
}
